<?php
class product_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	//======category=========//
	function getMaincat()
	{
		$this->db->where('ParentID',0);
		$this->db->where('bl_active',1);
		$this->db->where('IsMainMenu',1);
		$this->db->order_by('Ordering','ASC');  
		$query = $this->db->get('category');
		return $query->result();
	}

	function getNumProduct($cid)
	{
	  $ar_cid = $this->get_array_cat_search($cid);
	  $this->db->select('product.*,categoryproduct.*');
	  $this->db->join('categoryproduct','categoryproduct.ProductID = product.ProductID');
	  if($cid!=0)
	  {
		  $this->db->where_in('categoryproduct.CategoryID',$ar_cid);
	  }          
	  $query = $this->db->get('product');
	  return $query->num_rows();
	}

	function getAllProduct($cid, $num, $offset)
	{
		$ar_cid = $this->get_array_cat_search($cid);
		$this->db->select('product.*,categoryproduct.*');
		$this->db->join('categoryproduct','categoryproduct.ProductID = product.ProductID');
		if($cid!=0)
		{
			$this->db->where_in('categoryproduct.CategoryID',$ar_cid);
		}   
		
		$this->db->order_by('product.LastUpdate',"DESC");
		$this->db->order_by('product.Ordering',"ASC");
		$query = $this->db->get('product',$num,$offset);
		return $query->result();
	}

	function get_array_cat_search($CategoryID)
	{
	  $ar_cat = array($CategoryID);
	  $info_cat = $this->check_parentid($CategoryID);	// level 2
	  if(count($info_cat) > 0)
	  {
		  foreach($info_cat as $rs):
				array_push($ar_cat,$rs->CategoryID); 
				$info_cat1 = $this->check_parentid($rs->CategoryID);	// level 3
				if(count($info_cat1) > 0)
				{
					foreach($info_cat1 as $rs1):
					array_push($ar_cat,$rs1->CategoryID);
					$info_cat2 = $this->check_parentid($rs1->CategoryID);	// level 4
					if(count($info_cat2) > 0)
					{
						foreach($info_cat2 as $rs2):
						array_push($ar_cat,$rs2->CategoryID);
						endforeach;
					}   
					endforeach;
				}                 
		  endforeach;
	
	  }
	  return $ar_cat;
	}
	
	function check_parentid($CategoryID)
	{          
	  $this->db->where('ParentID',$CategoryID);
	  $this->db->order_by('Ordering','ASC');
	
	  $query = $this->db->get('category');
	  return $query->result();          
	}
	
	function get_img_by_session($session)
	{
	  $this->db->where('ImageSession',$session);
	  $query = $this->db->get('productimage');
	  return $query->result();
	}
	
	function saveProduct()
	{
	  $ProductID = (int)$this->uri->segment(3);
	  $CategoryID = (int)$this->input->post('CategoryID');
	  
	  $data = array(
		'ProductCode' => strtoupper($this->input->post('ProductCode')),
		'ProductName' => $this->input->post('ProductName'),
		'Alias' =>  strtolower(kodau($this->input->post('ProductName'))),
		'MenufactureID' => (int)$this->input->post('MenufactureID'),
		'Price' => to_number($this->input->post('Price')),
		'CategoryID' => (int)$this->input->post('CategoryID'),
		'VAT' 		=> (int)$this->input->post('VAT'),
		'ProductType' => (int)$this->input->post('ProductType'),
		'ProductKind' =>  $this->input->post('kind'),
		'Description' => (string)$this->input->post('Description'),
		'ProductFunc' => (string)$this->input->post('ProductFunc'),
		'ProductStatus' => (int)$this->input->post('ProductStatus'),
		'ProductStatusText' => $this->input->post('ProductStatusText'),
		
		'ProductSize' => (string)$this->input->post('ProductSize'),
		'ProductWeight' => $this->input->post('ProductWeight'),
		'ProductCapacity' => $this->input->post('ProductCapacity'),
		'ProductWarranty' => $this->input->post('ProductWarranty'),
		'PromotionActice' => $this->input->post('PromotionActice'),
		'PromotionInfo' => $this->input->post('PromationInfo'),
		'bl_active' => (int)$this->input->post('bl_active'),
		'IsHome' => (int)$this->input->post('IsHome'),
		'IsAllowShipping' => (int)$this->input->post('IsAllowShipping'),
		'NewPercent' => (int)$this->input->post('NewPercent'),
		'Phamvi' => (int)$this->input->post('Phamvi'),
		'ProductImg' => (string)$this->input->post('ProductImg'),
		'UserID' => 0,
		'LastUpdate' => date("Y-m-d H:i:s")
	  );
	
	  if($ProductID !=0)
	  {
		  // Update products
		  $this->db->where('ProductID',$ProductID);
		  if($this->db->update('product',$data))
		  {
			  // Update Product Category
			  $Data_Category_Product = array(
					'CategoryID' => $CategoryID
			  );
			  $this->db->where('ProductID', $ProductID);
			  $this->db->update('categoryproduct',$Data_Category_Product);
			  
			  return true;
		  }else{
			  return false;
		  }
	  }
	  else
	  {
		  // Add new products
		  if($this->db->insert('product',$data))
		  {
			  $ProductID_new = $this->db->insert_id();
			  
			  /// Insert Product Category
			  $Data_Category_Product = array(
				'CategoryID' => $CategoryID,
				'ProductID' => $ProductID_new
			  );
			  $this->db->insert('categoryproduct',$Data_Category_Product);
			  
			  // Insert Product Statics
			  $Data_Product_static = array(
				'ProductID' => $ProductID_new
			  );    
			  $this->db->insert('productstaticstic',$Data_Product_static); 
			  
			  $session_info = $this->session->userdata('session_id');
			  $list_img_se = $this->get_img_by_session($session_info);
				
			  if(count($list_img_se) > 0)
			  {                  
				  foreach($list_img_se as $rs):
					  fn_resize_image('../uploads/images/'.$rs->ImageName,'../uploads/products/200/'.$rs->ImageName,600,600);
					  fn_resize_image('../uploads/images/'.$rs->ImageName,'../uploads/products/100/'.$rs->ImageName,200,200);
					  fn_resize_image('../uploads/images/'.$rs->ImageName,'../uploads/products/40/'.$rs->ImageName,40,40);
				  endforeach;
			  }
			  $data_img = array(
				'ProductID' => $ProductID_new,
				'ImageSession' => ''
			  );
			  $this->db->where('ImageSession',$session_info);
			  $this->db->update('productimage',$data_img);
			  return true;
		  }
		  else
		  {
			  return false;
		  }
	  }
	}
	
	function get_img_by_product_id($ProductID)
	{
	  $this->db->where('ProductID',$ProductID);
	  return $this->db->get('productimage')->result();
	}
	
	function get_info_product($ProductID)
	{
	  $this->db->select('product.*,categoryproduct.*');
	  $this->db->join('categoryproduct','categoryproduct.ProductID = product.ProductID');
	  $this->db->where('product.ProductID',$ProductID);
	  $query = $this->db->get('product');
	  return $query->row();
	}
	
	function delproduct($id)
	{
		$this->db->where_in('ProductID',$id);
		$query = $this->db->delete('product');
		if($query)
		{
			$this->db->where('ProductID',$id);
			$this->db->delete('categoryproduct');
			$this->db->where('ProductID',$id);
			$this->db->delete('productdiscount');
/*			$this->db->where('ProductID',$id);
			$this->db->delete('productpromotion');                
			$this->db->where('ProductID',$id);
			$this->db->delete('productstaticstic');  
			$this->db->where('ProductID',$id);
			$this->db->delete('productrating');   
			$this->db->where('ProductID',$id);
			$this->db->delete('productimage');                 
			$this->db->where('ProductID',$id);
			$this->db->delete('productpromotion');    
			$this->db->where('ProductID',$id);
			$this->db->delete('productcomment');  
			$this->db->where('ProductID',$id);
			$this->db->delete('productgifts');                                                                         
*/			return true;
		}
		else
		{
			return false;
		}          
	}      
	
	
}
?>